package cloud.maque.auth.service.impl;

import cloud.maque.auth.entity.AuthUser;
import cloud.maque.auth.entity.AuthUserThrid;
import cloud.maque.auth.model.Oauth2User;
import cloud.maque.auth.service.CommUserService;
import cloud.maque.auth.service.UserService;
import cloud.maque.auth.service.UserThridService;
import cloud.maque.common.core.enums.LoginType;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class CommUserServiceImpl implements CommUserService {

    @Autowired
    UserThridService thridService;

    @Autowired
    UserService userService;

    @Override
    @Transactional
    public AuthUser CreateUserWithOauth(Oauth2User user, LoginType loginType) {


        AuthUser authUser = new AuthUser();
        authUser.setUserName(loginType.name() + "_" + user.getUserName());
        authUser.setUserId(loginType.name() + "_" + user.getUserId());
        authUser.setDisabled(0);

        userService.CreateUser(authUser);

        AuthUserThrid thrid = new AuthUserThrid();
        thrid.setThridId(IdUtil.fastUUID());

        thrid.setThridType(loginType.name());
        thrid.setThridName(loginType.getDesc());
        thrid.setUserId(authUser.getUserId());
        thrid.setThridUserId(user.getUserId());
        thrid.setThridUserName(user.getUserName());
        thrid.setThridNickName(user.getNickName());
        thrid.setCreateTime(new Date());

        thridService.SaveThrid(thrid);

        return authUser;
    }
}
